package org.foreningsgatan.mercenaries.manual.generator;


import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.foreningsgatan.mercenaries.model.GroundDecal;
import org.foreningsgatan.mercenaries.model.Vegetation;
import org.foreningsgatan.modular.common.logging.Log;
import org.foreningsgatan.modular.resources.ResourceManager;
import org.foreningsgatan.modular.terrain.model.SimpleMap;
import org.foreningsgatan.modules.glcore.frames.Frames;


/**
 * Copyright (c) 2008 Joakim Back
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * @author Joakim Back
 *
 */
public class ScatterObjects {
	private static Log log = new Log(ScatterObjects.class);
	
	private static ResourceManager sResources;

	private static Random sRandom;
	
	static {
		sResources = ResourceManager.getInstance();
		sRandom = new Random();
	}
	
	private static Frames getRandomFrame(String ... pKeys) {
		String vKey = pKeys[sRandom.nextInt(pKeys.length)];
		return (Frames) sResources.get(vKey);
	}
	
	public static List<Vegetation> scatter(SimpleMap map, Object obj,
			float pDensity, boolean castShadow, 
			boolean isAffectedByWind, String ... pKeys) {
		List<Vegetation> vegetation = new ArrayList<Vegetation>();
		
		int size = map.getSize();

		for (int y = 0; y < size; y++) {
			for (int x = 0; x < size; x++) {
				boolean isValid = (map.get(x, y) == obj);
				
				if (isValid && sRandom.nextDouble() < pDensity) {
					double vx = x + sRandom.nextDouble() - 0.5;
					double vy = y + sRandom.nextDouble() - 0.5;
					double scale = 0.75 + (sRandom.nextDouble() * 0.5);

					vegetation.add(new Vegetation(vx, vy, scale, 
							getRandomFrame(pKeys), castShadow, isAffectedByWind, sRandom.nextDouble()));
				}
			}
		}

		log.debug("Scattered %s objects", vegetation.size());

		return vegetation;
	}
	
	public static List<GroundDecal> scatterDecal(SimpleMap map, Object obj,
			float pDensity, String ... pKeys) {
		List<GroundDecal> vList = new ArrayList<GroundDecal>();

		int size = map.getSize();

		for (int y = 0; y < size; y++) {
			for (int x = 0; x < size; x++) {
				boolean isValid = (map.get(x, y) == obj);

				if (isValid && sRandom.nextDouble() < pDensity) {
					double vx = x + sRandom.nextDouble() - 0.5;
					double vy = y + sRandom.nextDouble() - 0.5;
					double scale = 0.75 + (sRandom.nextDouble() * 0.5);

					vList.add(new GroundDecal(vx, vy, scale, getRandomFrame(pKeys)));
				}
			}
		}

		log.debug("Scattered %s objects", vList.size());

		return vList;
	}
}
